package engine.newUi
{
	import com.greensock.TweenLite;
	
	import engine.data.BindManage;
	import engine.data.ModelLocator;
	import engine.data.ObjectData;
	import engine.data.Record;
	import engine.data.RoleVo;
	import engine.data.View;
	import engine.data.ViewObject;
	import engine.flag.ResourcesPath;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.tip.TipsMainLogic;
	import engine.tip.tipItem.MarrowItem;
	import engine.ui.AlertUI;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.Timer;
	
	import newx.BaseObject;
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.com.control.NxClip;
	import newx.com.control.comboBox.NxComboBox;
	import newx.com.control.comboBox.NxComboBoxEvent;
	import newx.com.control.list.NxList;
	import newx.com.control.table.NxTabelPos;
	import newx.com.control.table.NxTable;
	import newx.com.control.text.NxLabel;
	import newx.com.control.window.NxWindowEvent;

	/**
	 * 灌顶（传承）
	 * @author yanghr
	 * 
	 */
	public class ImpartPropertyLogic
	{
		private static var _self:ImpartPropertyLogic;
		/**
		 * enumTypeInt：
		 * IMPART:int=1 灌顶
		 * BEIMPART:int=2; 被灌顶
		 */		
		private const IMPART:int=1,BEIMPART:int=2;
		/**
		 * STA 	体魄 2 
		 * STR	外功 0  
 		 * ING	内功 1
		 */		
		private const STA:int=2,STR:int=0,ING:int=1,PRO_MAP:Object={StrAdd:STR,StaAdd:STA,IngAdd:ING};
		/**
		 *遍历和属性映射用 
		 */		
		private var _intensifyMap:Object={};
		private var _ui:ImpartPropertyUI;
		private var _bindFlag:int=-1;
		private var _bindVIPflag:int=-1;
		/**
		 *当前被选中灌顶的人 
		 */		
		private var _currentView:ObjectData;
		/**
		 *当前被选中被灌顶的人 
		 */		
		private var _currentBeView:ObjectData;
		
		private var _bind:BindManage;
		private var _model:ModelLocator
		private var _role:RoleVo;
		private var _tip:TipsMainLogic;
		
		private var imFlag:int=-1,beFlag:int=-1;
		/**
		 *洗髓层数 
		 */		
		private const MAX_MARROW:int=6
		private const DANYAO_INDEX:int=2;
		private const INDEX:String="index";
		private const DATA:String="data";
		private const ANIMATION:String="animation",PROPERTY:String="propertyComplete",MARROW:String="marrowComplete";
		
		private const SELECT_TIPS:String=StaticIni.getSwfLangStr("ImportStr_16");
		private const NOTHING_STR:String=StaticIni.getSwfLangStr("ImportStr_28");
		/**
		 *储存临时变量... 
		 */		
		private var _currentObj:Object={};
		private var _currentCost:String="";
		private var _afterLevel:int;
		private var _roleArr:Array;
		/**
		 *当前灌顶对象 （左）
		 * @param value
		 * 
		 */
		/**
		 * 存丹药
		 */		
		private var _tmerArr:Array ; 
		public function set currentImData(value:ObjectData):void 
		{
			_currentView=value;
			refreshBaseInfo(_currentView,IMPART).refreshGroup(_currentView,IMPART).refreshTable(_currentView,IMPART);
		}
		private function unbindIm():void
		{
			imFlag=_bind.UnbindProp(_currentView,"CanInherit",imFlag);
		}
		private function bundIm():void
		{
			_currentObj[IMPART]=true;
			imFlag=imFlag==-1?_bind.BindProp(_currentView,"CanInherit",this,"inherit"):imFlag;
			_currentObj[IMPART]=false;
		}
		/**
		 *灌顶 
		 * @param value
		 * 
		 */		
		public function set inherit(value:String):void 
		{
//			trace("ImpartPropertyLogic.inherit(value)",value);
			if(!_currentObj[IMPART]&&value)
			{
				_currentObj[DATA]++;
//				refreshCombox();
				unbindIm()
				checkMsgBox()
			}
		}
		/**
		 *被灌顶 
		 * @param value
		 * 
		 */		
		public function set beinherit(value:String):void 
		{
//			trace("ImpartPropertyLogic.beinherit(value)",value);
			if(!_currentObj[BEIMPART]&&value)
			{
				_currentObj[DATA]++;
//				refreshCombox();
				unbindBe();
				checkMsgBox()
			}
		}
		/**
		 **灌顶传功成功* 
		 * 
		 */		
		private function checkMsgBox():void
		{
//			trace("ImpartPropertyLogic.checkMsgBox()",_currentObj[DATA]==2	,_currentObj[ANIMATION]	,	_currentObj[MARROW]);
			
			if(_currentObj[DATA]==2	&&	_currentObj[ANIMATION]/*&&	_currentObj[MARROW]*/)
			{
				refreshGroup(_currentBeView,BEIMPART);
				AlertUI.showAlert("",StaticIni.getSwfLangStr("ImportStr_27"),AlertUI.OK, function onAddBlackNameBack(type:int, str:String, clickData:*):void{
					close();
				})
			}
		}
		/**
		 *等级元宝消耗 
		 * @return 
		 * 
		 */		
		private function get magicCoinCost():int
		{
			return StaticIni.getIniObj("BaseInfo")["MagicCoinConsume_level"];
		}
		/**
		 *丹药元宝消耗 
		 * @return 
		 * 
		 */		
		private function get magicCoinCost1():int
		{
			return StaticIni.getIniObj("BaseInfo")["MagicCoinConsume_pill"];
		}
		/**
		 *确认灌顶？ 
		 * 
		 */		
		private function makeSureBox():void
		{
			var mainStr:String=StaticIni.getSwfLangStr("ImportStr_27");
			var id:String;
			var param:Array;
			var goldCoin:String;
			if(!_currentObj["exploit"]&&!_currentObj["coin"])
			{
				NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("99223"));
				return ;
			}
			
			if(exchangeType || exchangeType1)
			{
				var goldCoin1 : int = exchangeType?magicCoinCost:0;
				var goldCoin2 : int = exchangeType1?magicCoinCost1:0;
				goldCoin = String(goldCoin1+goldCoin2);
				if(_currentObj["coin"])
				{
					if(_currentObj["exploit"])
					{
						id="ImportStr_23";
						param=[_currentObj["coin"],_currentObj["exploit"],goldCoin]
					}
					else
					{
						id="ImportStr_25";
						param=[_currentObj["coin"],goldCoin];
					}
				}
				else
				{
//					if(_currentObj["exploit"])
//					{
						id="ImportStr_24";
						param=[_currentObj["exploit"],goldCoin];
//					}
//					else
//					{
//						
//					}
				}
			}
			else
			{
				id="ImportStr_26";
				param=[_currentObj["coin"],_currentObj["exploit"]];
			}
			mainStr=StaticIni.getSwfLangStrVar(id,param);
			AlertUI.showAlert("",mainStr,AlertUI.OK|AlertUI.CANCEL, function onAddBlackNameBack(type:int, str:String, clickData:*):void{
				if(type==AlertUI.OK)
				{
					commitImpart()
				}
			});
		}
		/**
		 *得到消耗 
		 * @param e
		 * 
		 */		
		private function getConsume(e:CustomMsgEvent):void
		{
			var exploit:int=e.pList.shift();
			var coin:int=e.pList.shift();
			_currentObj["coin"]=coin;
			_currentObj["exploit"]=exploit;
			var cointStr:String=coin+"";
			var explStr:String=exploit+"";
			if(coin>_role.getGoldCoin()||exploit>_role.GetPropertiesEx("Exploit"))
			{
				_ui.commitBtn.enabled=false;
				_ui.commitBtn.mouseEnabled=true;
				if(coin>_role.getGoldCoin())
				{
					cointStr=_tip.getColorHtml(	coin+"","ff0000");
				}
				else
				{
					explStr=_tip.getColorHtml(exploit+"","ff0000");
				}
			}
			else
			{
				_ui.commitBtn.enabled=true;
			}
			_currentCost=StaticIni.getSwfLangStrVar("ImportStr_22",[cointStr,explStr]);
		}
		/**
		 *当前被灌顶对象 （右）
		 * @param value
		 * 
		 */		
		public function set currentBeData(value:ObjectData):void
		{
			_currentBeView=value;
			if(value==null)
			{
//				_ui.label_4 .visible = false ; 
//				_ui.label_3.visible  = false ; 
//				_ui.line_3 . visible = false ; 
//				_ui.line_4 .visible  = false ; 
				_ui.mc_3.visible = false ; 
				_ui.mc_4.visible = false ; 
				_ui.bglab .visible   = true ; 
				
			}else
			{
//				_ui.label_4 .visible = true ; 
//				_ui.label_3.visible  = true ; 
//				_ui.line_3 . visible = true ; 
//				_ui.line_4 .visible  = true ; 
				_ui.mc_3.visible = true ; 
				_ui.mc_4.visible = true ; 
				_ui.bglab .visible  =  false ; 
			}
			refreshBaseInfo(_currentBeView,BEIMPART).refreshGroup(_currentBeView,BEIMPART).refreshTable(_currentBeView,BEIMPART);
		}
		/**
		 *删除绑定 
		 * 
		 */		
		private function unbindBe():void
		{
		
			beFlag=_bind.UnbindProp(_currentBeView,"CanInherited",beFlag);
		}
		/**
		 *绑定 
		 * 
		 */		
		private function bundBe():void
		{
			_currentObj[BEIMPART]=true;
			beFlag=beFlag==-1?_bind.BindProp(_currentBeView,"CanInherited",this,"beinherit"):beFlag;
			_currentObj[BEIMPART]=false;
		}
		public function get ui():ImpartPropertyUI
		{
			if(!_ui)
			{
				_ui=new ImpartPropertyUI()
			}
			return _ui;
		}
		public static function getInstance():ImpartPropertyLogic
		{
			if(_self==null)
			{
				_self=new ImpartPropertyLogic();
			}
			return _self;
		}
		private function initRoleArr():void
		{
			_roleArr=[];
		}
		/**
		 *根据伙伴打开 
		 * @param viewobj
		 * 
		 */		
		public function showWithPartner(viewobj:ObjectData):void
		{
			_ui.show();
			var arrr:Array=_ui.partnerComb.dataProvider.filter(
				function(item:Object,index:int,arr:Array):Boolean
				{
					return	item.data==viewobj;
				})
			if(arrr.length>0)
			{
				_ui.partnerComb.setSelected(_ui.partnerComb.dataProvider.indexOf(arrr[0]));
				_ui.partnerComb.dispatchEvent(new NxComboBoxEvent(NxComboBoxEvent.NXCOMBOBOX_CLICK,_ui.partnerComb.getSelecedItem()));
			}
			else
			{
//				trace("ImpartPropertyLogic.showWithPartner(viewobj)",_ui.partnerComb.dataProvider);
			}
			
		}
		public function  Init():void
		{
			_bind=BindManage.getInstance();
			_model=ModelLocator.getInstance();
			_role=_model.scene.Role;
			_tip=TipsMainLogic.instance;
			_ui.jiantou["_x"]=_ui.jiantou.x;
			_ui.jiantou["_y"]=_ui.jiantou.y;
//			_ui.commitBtn.label=StaticIni.getSwfLangStr("ImportStr_18");
//			_ui.co.label=StaticIni.getSwfLangStr("ImportStr_18")
//			getComboxLab(IMPART).htmlText=	getComboxLab(BEIMPART).htmlText=SELECT_TIPS;
//			_intensifyMap[STA]=["ImportStr_15",ActorPropertyUI.STA_WORK,"SkinTotal",ActorPropertyUI.STA_ADD];
//			_intensifyMap[ING]=["ImportStr_14",ActorPropertyUI.INTER_WORK,"BoneTotal",ActorPropertyUI.INTER_ADD];
			_intensifyMap[STR]=["ImportStr_30",ActorPropertyUI.OUT_WORK,"MuscleTotal",ActorPropertyUI.OUT_ADD];
			
			_model.addEventListener(CustomToClientFlag.SERVER_CUSTOMMSG_WASH_PROPNAME_AND_VALUE+"",getExchangeObj,false,-1);
			_model.addEventListener(CustomToClientFlag.SERVER_CUSTOMMSG_INHERITE_CONSUME+"",getConsume);
			
		}
		/**
		 *刷新属性 
		 * @param e
		 * 
		 */		
		private function getExchangeObj(e:CustomMsgEvent):void
		{
			if(_ui.created	)
			{
				if(_currentView	&&	e.pList[0]==_currentView.ObjKey)
				{
					refreshGroup(_currentView,IMPART)
				}
				else if(_currentBeView	&&	e.pList[0]==_currentBeView.ObjKey)
				{
					if(_currentObj[ANIMATION+INDEX])
					{
						_currentObj[MARROW]=true;
					}
					else
					{
						refreshGroup(_currentBeView,BEIMPART);
						if(!viewBeInherit(_currentBeView))
						{
							calculateGroup()
						}
					}
				}
			}
			
		}
		public function	BeforeShow():void
		{
			
			_ui.mc_1.source = ResManager.newResClass("IntensifyUIPic") ; 
			_ui.mc_2.source = ResManager.newResClass("DoMarrowUIPic") ; 
			_ui.mc_3.source = ResManager.newResClass("IntensifyUIPic") ; 
			_ui.mc_4.source = ResManager.newResClass("DoMarrowUIPic") ; 
			//DoMarrowUIPic
			_currentObj[DATA]=0;
			_ui.commitBtn.enabled=true;
			initRoleArr()
//			WashMarrowLogic.getInstance().requestALlMarrowData();
			bindAll();
			addAllEventListener();
			refreshCombox()
			currentBeData=null;
			refreshSingleCombox(IMPART);
		}
		public function	AfterHide():void
		{
			_currentCost="";
			_currentObj[DATA]=0;
			_currentObj[MARROW]=false;
			_currentObj[ANIMATION]=false;
			DoMarrowLogic.getInstance().resetTansmit();
			removeAllEventListener();
			_ui.partnerComb2.data.DeleteProperties(DATA);
			unbindAll();
			_ui.mc_1.source  =null ; 
			_ui.mc_2.source  =null ; 
			_ui.mc_3.source  =null ; 
			_ui.mc_4.source  =null ; 
			_ui.seletedMc.gotoAndStop(1);
			_ui.seleted1Mc.gotoAndStop(1);
			
		}
		private function unbindAll():void
		{
			if( _bindFlag != -1 )
			{
				_bindFlag		=	_bind.UnbindView(View.VIEWPORT_PARTNER,_bindFlag);
			}
		}
		private function bindAll():void
		{
			if( _bindFlag == -1 )
			{
				_bindFlag		=	_bind.BindView(_model.scene,View.VIEWPORT_PARTNER,OnAddPartner,OnRemovePartner);
			}
//			_bind.BindRecord(_model.scene,"PartnerPillUseRec",onaddPerHandle,onremovePerHandle)
			_bindVIPflag=_bindVIPflag==-1?_bind.BindProp(_role,"VIPLevel",this,"vipLevel"):_bindVIPflag;
		}
//		/**
//		 * 使用丹药成功
//		 */	
//		public function onaddPerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
//		{
//			 _tmerArr=PropertiesValue
//		}
//		/**
//		 *在这里用不到 
//		 */		
//		public function onremovePerHandle(PropertiesName:String,PropertiesValue:Array,data:*):void
//		{	
//			
//		}
		public function set vipLevel(value:int):void 
		{
			var v:Boolean=_ui.seletedMc.visible=_ui.seletedLab.visible=(value>=int(StaticIni.getIniObj("ImpartUI_Need")["selected_VIP"]));
			var v1:Boolean=_ui.seleted1Mc.visible=_ui.seleted1Lab.visible=(value>=int(StaticIni.getIniObj("ImpartUI_Need")["selectedpill_VIP"]));
			if(!v)
			{
				_ui.seletedMc.gotoAndStop(1);
			}
			if(!v1)
			{
				_ui.seleted1Mc.gotoAndStop(1);
			}
				
		}
		private function OnAddPartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			_roleArr.push(PropertiesValue);
		}
		private function OnRemovePartner(PropertiesName:String,PropertiesValue:ViewObject,data:*):void
		{
			var index:int=_roleArr.indexOf(PropertiesValue);
			if(index>-1)
			_roleArr.splice(_roleArr.indexOf(PropertiesValue),1)
		}
		public function addAllEventListener():void
		{
			_ui.windowBG.addEventListener(MouseEvent.CLICK,clickHandle,false,-1);
			_tip.objRollshowTips(_ui.commitBtn,function():String{
				return _currentCost
			})
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,close);
			_ui.windowBG.addEventListener(NxComboBoxEvent.NXCOMBOBOX_CLICK,comboxHandle,true)
		}
		/**
		 *combox选择后的回调 
		 * @param e
		 * 
		 */		
		private function comboxHandle(e:NxComboBoxEvent):void
		{
			var combox:NxComboBox=e.target as NxComboBox;
			var	enumtype:int
			var data:ObjectData=e.item.data;
			combox.data.HandleProperties(DATA,combox.getSelecedItem());
			WashMarrowLogic.getInstance().checkAndSendRequest(data,-1)
			if(combox==_ui.partnerComb)
			{
				enumtype	=	IMPART;
				currentImData=data;
				(_ui.partnerComb2.data as BaseObject).DeleteProperties(DATA)
				currentBeData=null;
				refreshSingleCombox(BEIMPART);
			}
			else
			{
				enumtype	=	BEIMPART;
				currentBeData	=	data;
				if(_currentView)
				{
					calculateBaseInfo();
					calculateGroup();
					calculateTable();
					requestForCost();
				}
			}
		}
		/**
		 *得到是否可以灌顶 
		 * @param item
		 * @return 
		 * 
		 */		
		public function viewInherit(item:ObjectData):int
		{
			return int(item.GetPropertiesEx("CanInherit"));
		}
		/**
		 *得到是否可以被灌顶 
		 * @param item
		 * @return 
		 * 
		 */		
		public function viewBeInherit(item:ObjectData):int
		{
			return int(item.GetPropertiesEx("CanInherited"));
		}
		/**
		 *刷新下拉菜单数据 
		 * @return 
		 * 
		 */		
		private function refreshCombox():ImpartPropertyLogic
		{
			var arr1:Array=_roleArr.filter(function(item:ObjectData,index:int,arr:Array):Boolean{
//				return true
				return !viewInherit(item)
			})
			var arr2:Array=_roleArr.filter(function (item:ObjectData,index:int,arr:Array):Boolean{
//				return true
				return !viewBeInherit(item);
			})
			constructCombox(arr1,IMPART);
			constructCombox(arr2,BEIMPART);
			return this;
		}
		/**
		 *刷新指定下拉菜单数据 
		 * @param enumTypeInt
		 * 
		 */		
		private function refreshSingleCombox(enumTypeInt:int):void
		{
			var arr:Array=_roleArr.filter(function (item:ObjectData,index:int,arr:Array):Boolean{
				return enumTypeInt==IMPART	?	!viewInherit(item)&&item!=_currentBeView	:	!viewBeInherit(item)&&item!=_currentView;
			})
			constructCombox(arr,enumTypeInt);
		}
		private function arrSortClosure(item1:ObjectData,item2:ObjectData):int
		{
			return item2.GetPropertiesEx(ActorPropertyUI.LEVEL)-item1.GetPropertiesEx(ActorPropertyUI.LEVEL);
		}
		/**
		 *构建下拉菜单 
		 * @param data
		 * @param enumTypeInt
		 * 
		 */		
		private function constructCombox(data:Array,enumTypeInt:int):void
		{
			var comb:NxComboBox=getCombox(enumTypeInt);
			comb.clear();
			data.sort(arrSortClosure)
			data.forEach(eachItem);
			comb.showCount=data.length;
			comb.title=SELECT_TIPS;
			if(comb.data.CheckProperties(DATA))
			{
				var obj:Object=comb.data.GetProperties(DATA)
				var currentSelect:int;
				comb.dataProvider.every(function(item:Object,index:int,arr:Array):Boolean
				{
//					trace("ImpartPropertyLogic.enclosing_method(item, index, arr)",obj["label"]);
					var b:Boolean=item["data"]!=obj["data"];
					currentSelect=comb.dataProvider.indexOf(item);
					return b
				}
				);
				if(currentSelect>-1)
				{
					comb.setSelected(currentSelect);
	//				comb.dispatchEvent(new NxComboBoxEvent(NxComboBoxEvent.NXCOMBOBOX_CLICK,comb.getSelecedItem()));
				}
			}
			else
			{
//				setComboxLabVisible(enumTypeInt,true);
//				obj={label:SELECT_TIPS,data:null};
//				comb.addItem(obj)
//				comb.getSelecedItem()["label"]="请选择";	
//				comb.setSelected(comb.dataProvider.indexOf(obj));
			}
			function eachItem(item:ObjectData,index:int,arr:Array):void
			{
//				_text.htmlText=StaticIni.getSwfLangStr(item+"")
				//				trace("MyPropertyLogic.eachItem(item, index, arr)",_text.text);
				if(!(comb==_ui.partnerComb2&&item==_currentView)&&!(comb==_ui.partnerComb&&item==_currentBeView))
					comb.addItem({label:getObjDataName(item)+" "+getObjDataLevel(item),data:item});
			}
		}
		private function getObjDataName(dataO:ObjectData):String
		{
			return	StaticIni.getSwfLangStr(dataO.GetPropertiesEx(ActorPropertyUI.CONFIG_ID));
//			return	(dataO is RoleVo)?dataO.GetPropertiesEx(ActorPropertyUI.NAME):StaticIni.getSwfLangStr(dataO.GetPropertiesEx(ActorPropertyUI.CONFIG_ID));
		}
		private function getObjDataLevel(dataO:*):String
		{
			return	StaticIni.getSwfLangStrVar("UIString_1000015",[dataO is ObjectData?dataO.GetPropertiesEx(ActorPropertyUI.LEVEL):dataO]);
		}
		/**
		 *刷新基本信息 
		 * @param data
		 * @param enumTypeInt
		 * @return 
		 * 
		 */		
		private function refreshBaseInfo(data:ObjectData,enumTypeInt:int):ImpartPropertyLogic
		{
			var face:NxClip=getFaceClip(enumTypeInt);
			var name:NxLabel=getNameLab(enumTypeInt);
			var lvl:NxLabel=getLvlLab(enumTypeInt);
			if(data)
			{
				face.source=ResourcesPath.getHeadPic(data.GetPropertiesEx(ActorPropertyUI.MODEL),data.GetPropertiesEx(ActorPropertyUI.CLOTHE_MODEL));
				name.htmlStr=getObjDataName(data);
				lvl.htmlStr=getObjDataLevel(data)//data.GetPropertiesEx(ActorPropertyUI.LEVEL);
			}
			else
			{
				face.source=null;
				name.htmlText="";
				lvl.htmlText=""
			}
			return this
		}
		/**
		 *计算基本属性在洗髓后的加成（右边） 
		 * 
		 */		
		private function calculateBaseInfo():void
		{
			var base1:int=_currentView.GetPropertiesEx(ActorPropertyUI.LEVEL);//左边的
			var base2:int=_currentBeView.GetPropertiesEx(ActorPropertyUI.LEVEL);//右边的
			var addI:int=arithmeticLevel(base1,base2);// 被传承的等级
			getLvlLab(BEIMPART).htmlText=addStr(base2,addI);
			_afterLevel= base2+addI;
			function addStr(base:int,add:int):String
			{
				if(add>base)
					return getObjDataLevel(base)+"("+getObjDataLevel(add)+")";
				return getObjDataLevel(base);
			}
		}
		/**
		 *等级增加算法 ---
		 * @param base1
		 * @param base2
		 * @return 
		 * 
		 */		
		private function arithmeticLevel(base1:int,base2:int):int
		{
			var mult:Number
			if(_ui.seletedMc.currentFrame==1)
				mult= StaticIni.getIniObj("Proportion")["BasePercentage1"];
			else
				mult= StaticIni.getIniObj("Proportion")["VIPPercentage1"];
			return (base1)*mult
		}
		/**
		 * 刷新丹药属性
		 * @param data
		 * @param enumTypeInt
		 * @return 
		 * 
		 */		
		private function refreshTable(data:ObjectData,enumTypeInt:int):ImpartPropertyLogic
		{
			var nxtable:NxTable=getPropertyTable(enumTypeInt);
			var arr:Array  = [] ; 
			var configid:int =  0 ;
//			if(nxtable.dataProvider.length!=3)
//			{
//				nxtable.dataProvider=new Array(3);
//			}
			if(_model.scene.CheckProperties("PartnerPillUseRec"))
			{
				_tmerArr=[];
				_tmerArr= (_model.scene.GetPropertiesEx("PartnerPillUseRec") as Record).recArr;
			}
			if(data!=null)
			{
				configid =  data["Properties"].ConfigID ; 
				var i :int =  0 ; 
				for(i ; i <_tmerArr.length ; i++)
				{
					 if (configid == _tmerArr[i][3])
					 {
						 arr.push(_tmerArr[i]); 
					 }
				}
				
			}
			/**
			 * STR	外功 0  
			 * ING	内功 1
			 * STA 	体魄 2 
			 * */
			var  str:int  = 0  ;
			var  ing:int  = 0  ; 
			var  sta:int  = 0 ; 
			for(var j:int = 0;  j<arr.length ;j++)
			{
				if(1 == arr[j][2])
				{
					str += arr[j][1]
					
				}else if (2 == arr[j][2])
				{
					ing += arr[j][1]
						
				}else if (3 == arr[j][2])
				{
					sta += arr[j][1] ;
				}
				
			}
			
			nxtable.visible=Boolean(data);
			if(data)
			{
				for  (var k:String in _intensifyMap) 
				{ 
					var d:Array=_intensifyMap[k];
					var proName:String=StaticIni.getSwfLangStrVar(d[0],[""])
//					var base:Number=data.GetPropertiesEx(d[1]);
//					var add:Number=data.GetPropertiesEx(d[3]);
//					nxtable.setLineByIndex([proName,base,data.GetPropertiesEx(d[DANYAO_INDEX]),(base+add)],int(i));
					nxtable.setLineByIndex([proName,str,ing,sta],int(k));
				}
			}
			return this
		}
		/**
		 *计算丹药加成 
		 * 
		 */		
		private function calculateTable():void
		{
			var result:Object=ransackPillUse();
			var arr1:Array=getPropertyTable(IMPART).dataProvider;
			var arr2:Array=getPropertyTable(BEIMPART).dataProvider;
			for  (var iString:String in result) 
			{
				var i:int=PRO_MAP[iString];
				var arrr:Array=arr1[i];
				var base:int=getNum(arrr)	;
				var add:int	=	result[iString]///arithmeticProperty(base,getNum(arr2[i]));
				var totalStr:String	=	getAddPROString(arr2[i][int(iString)+1].label,add);
				arr2[i][int(iString)+1].label=totalStr;
			}
			getPropertyTable(BEIMPART).dataProvider=arr2;
			if(_ui.seleted1Mc.currentFrame == 2)
			{
				for(var j:int=1;j<arr1[0].length;j++)
				{
					arr2[0][j].label = int(arr2[0][j].label)+"("+arr1[0][j].label+")";
				}
				getPropertyTable(BEIMPART).dataProvider = arr2;
			}
			function getNum(__arr:Array):Number
			{
				return isNaN(__arr[DANYAO_INDEX].label)?__arr[DANYAO_INDEX].label.split("+")[0]:Number(__arr[DANYAO_INDEX].label)
			}
		}
		/**
		 *内功丹药  衰减算法 
		 * @param base
		 * @return 
		 * 
		 */		
		private function arithmeticProperty(base1:int,base2:int):int
		{
			var mult:Number
//			if(_ui.seletedMc.currentFrame==1)
				mult= StaticIni.getIniObj("Proportion")["BasePercentage2"];
//			else
//				mult= StaticIni.getIniObj("Proportion")["VIPPercentage2"];
			return (base1)*mult-base2 // 向下取整
		}
		/**
		 *map[PillType][PillId]=UsedTimes 
		 */		
		private function ransackPillUse():Object
		{
			var recArr:Array=_model.scene.GetRecordEx("PartnerPillUseRec").recArr;
			var map1:Object={};
			var map2:Object={};
			var result:Object={};
			for each (var iArr:Array in recArr) 
			{
				if(iArr[3]==_currentView.GetPropertiesEx("ConfigID"))//灌顶的人
				{
					mapAdd(map1);
				}
				if(iArr[3]==_currentBeView.GetPropertiesEx("ConfigID"))//被灌顶的人
				{
					mapAdd(map2);
				}
			}
			for  (var iType:String in map1) // 按照丹药类型循环
			{
				if(!map2.hasOwnProperty(iType))
				{
					map2[iType]={};
				}
				var typeMap:Object=map1[iType];
				var count1:int ; 
				var count2:int ; 
				var addCount:int = 0; 
				for  (var id:String in typeMap) // 这个类型吃了几个丹药
				{
					if(!map2[iType].hasOwnProperty(id))//被传承的人物身上有没有丹药
					{
						map2[iType][id]=0;
					}
					 count1 = typeMap[id];
					if(typeMap[id]<=1){
						if(!result.hasOwnProperty((int(iType)-1).toString()))
						{
//							result[(int(iType)-1).toString()]=0
						}
						continue;
					}
					var configobj:Object=StaticIni.getIniObj(id);
					var typeName:String=configobj["AddPropertyName"];
					 count2 = map2[iType][id];
					 addCount += arithmeticProperty(count1,count2)//int((count1-count2)*multConfig);
					 if(count1-count2>0	&&	addCount>0/*	&&	configobj["NeedLevel"]<=_afterLevel*/)
					 {
						 var value1:int=configobj["AddPropertyValue"]
	 					var value2:int=arithmeticResult(value1,configobj["WeakenValue"],count2,addCount);
						 result[(int(iType)-1).toString()]=addCount//arithmeticProperty(count1,count2);
					 }
				}
				
			}
			return result;
			//map[类型] [丹药ID] = 个数
			function mapAdd(map:Object):void
			{
				if(!map.hasOwnProperty(iArr[2]))
				{
					map[iArr[2]]={}
				}
				map[iArr[2]][iArr[0]]=iArr[1];
			}
			function arithmeticResult(totol:int,weak:int,cCount:int,aCount:int):Number
			{
				var t:int;
				for (var i:int = 0; i < aCount; i++) 
				{
					t+=totol-weak*(cCount+i)
				}
//				trace("ImpartPropertyLogic.calculateResult(totol, weak, cCount, aCount)",totol,weak,cCount,aCount,t);
				return	t; 
			}
			
		}
		/**
		 *计算洗髓属性（右边） 
		 * 
		 */		
		private function calculateGroup():void
		{
			var len:int=MAX_MARROW;
			for (var i:int = 0; i < len; i++) 
			{
				var col:int=(i%3)
				for (var j:int = 0; j < 3; j++) 
				{
					var tableBe:NxTable=getTable(BEIMPART);
					var obj1:Object=getTableIndexData(getTable(IMPART),col,j);
					var obj2:Object=getTableIndexData(tableBe,col,j);
					var value:Number=arithmeticMarrow((obj1["data"]["num"]),(obj2["data"]["num"]));
					var propertyName:String=obj1["data"]["property"];
					var b:Boolean=MarrowItem.checkInt(propertyName);
					value=b?int(value):Number(value.toFixed(2))
					var label:String=getAddPROString(obj2["label"],value,!b);
					changeTableIndexData(getTable(BEIMPART),col,j,label,obj2["data"]);
				}
			}
			function getTable(enumType:int):NxTable
			{
				if(i>=3)
				{
					return getMarrow2Table(enumType)
				}
				return getMarrow1Table(enumType);
			}
		}
		/**
		 *洗漱属性 衰减算法 
		 * @param base
		 * @return 
		 * 
		 */		
		private function arithmeticMarrow(base1:Number,base2:Number):Number
		{
			var mult:Number
			if(_ui.seletedMc.currentFrame==1)
				mult= StaticIni.getIniObj("Proportion")["BasePercentage3"];
			else
				mult= StaticIni.getIniObj("Proportion")["VIPPercentage3"];
			return (base1-base2)*mult
		}
		/**
		 *刷新洗髓属性 
		 * @param data
		 * @param enumTypeInt
		 * @return 
		 * 
		 */		
		private function refreshGroup(data:ObjectData,enumTypeInt:int):ImpartPropertyLogic
		{
			getMarrow1Table(enumTypeInt).visible=Boolean(data);
			getMarrow2Table(enumTypeInt).visible=Boolean(data);
			if(!data)
			{
				return this;
			}
			var table:NxTable=getMarrow1Table(enumTypeInt);
			var len:int=MAX_MARROW;
			var marrow:MarrowItem=new MarrowItem(data,0);
			for (var i:int = 0; i < len; i++) 
			{
				marrow.data=i+1;
				if(i==3)
					table=getMarrow2Table(enumTypeInt);
//				var mc:MovieClip=group.getItemByIndex(i);
				var arr:Array=marrow.getPropertiesColorArr();
				if(arr)
				{
					for (var j:int = 0; j < arr.length; j++) 
					{
						changeTableIndexData(table,i>=3?i-3:i,j,arr[j],{property:arr["index"][j],num:arr["data"][arr["index"][j]]});
					}
				}
				else
				{
					for ( j = 0; j < 3; j++) 
					{
						changeTableIndexData(table,i>=3?i-3:i,j,NOTHING_STR,{property:"",num:0});
					}
				}
				
			}
			return this
		}
		private function changeTableIndexData(table:NxTable,col:int,row:int,label:String,data:*):void
		{
			var nxpos:NxTabelPos=new NxTabelPos(row,col);
			var arr:Array=table.dataProvider;
			if(!arr.hasOwnProperty(row))
			{
				arr[row]=[];
			}
			if(!arr[row].hasOwnProperty(col))
			{
				arr[row][col]={};
			}
			arr[row][col]["data"] = data;			
			arr[row][col]["label"] = label;
//			table.setItemDataFrom(nxpos,data);
//			table.setItemFrom(nxpos,label);
			table.dataProvider=arr;
		}
		private function getTableIndexData(table:NxTable,col:int,row:int):Object
		{
			var nxpos:NxTabelPos=new NxTabelPos(row,col);
			var label:String=table.getItemFrom(nxpos);
			var data:Object=table.getItemDataFrom(nxpos);
			return {label:label,data:data}
		}
		/**
		 *都是根据 enumTypeInt得到相应的组件
		 * @param enumTypeInt
		 * @return 
		 * 
		 */		
		private function getCombox(enumTypeInt:int):NxComboBox
		{
			if(enumTypeInt==IMPART)
				return _ui.partnerComb
			return _ui.partnerComb2;
		}
		private function getGangLab(enumTypeInt:int):NxLabel
		{
			if(enumTypeInt==IMPART)
				return _ui.gangLab
			return _ui.gangLab2;
		}
		private function getLvlLab(enumTypeInt:int):NxLabel
		{
			if(enumTypeInt==IMPART)
				return _ui.lvlLab;
			return _ui.lvlLab2;
		}
		private function getNameLab(enumTypeInt:int):NxLabel
		{
			if(enumTypeInt==IMPART)
				return _ui.nameLab;
			return _ui.nameLab2;
		}
		private function getFaceClip(enumTypeInt:int):NxClip
		{
			if(enumTypeInt==IMPART)
				return _ui.faceClip;
			return _ui.faceClip2;
		}
		private function getMarrow1Table(enumType:int):NxTable
		{
			if(enumType==IMPART)
				return _ui.marrow1table1
			return _ui.marrow1table2;
		}
		private function getMarrow2Table(enumType:int):NxTable
		{
			if(enumType==IMPART)
				return _ui.marrow2table1
			return _ui.marrow2table2;
		}
		private function getPropertyTable(enumTypeInt:int):NxTable
		{
			if(enumTypeInt==IMPART)
				return _ui.propertytable;
			if(enumTypeInt==BEIMPART)
				return _ui.propertytable2;
			else return null;
		}
		public function removeAllEventListener():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,close)
		}
		/**
		 *单击回调 
		 * @param e
		 * 
		 */		
		private function clickHandle(e:Event):void
		{
			var target:DisplayObject=e.target as DisplayObject
			if(_ui.commitBtn.enabled&&_ui.commitBtn.contains(target))
			{
//				commitImpart()
				makeSureBox();
			}
			else if(_ui.seletedMc.contains(target)	||	_ui.seletedLab.contains(target))
			{
				changeSeleted();
			}
			else if(_ui.seleted1Mc.contains(target)	||	_ui.seleted1Lab.contains(target))
			{
				changeSeleted1();
			}
			else if(_ui.partnerComb.contains(target)&&!(target.parent is NxList))
			{
				refreshSingleCombox(IMPART)
			}
			else if(_ui.partnerComb2.contains(target)&&!(target.parent is NxList))
			{
				refreshSingleCombox(BEIMPART);
			}
		}
		private function changeSeleted():void
		{
			if(!exchangeType)
			{
				var magicCoin : int = (exchangeType1) ? magicCoinCost+magicCoinCost1 : magicCoinCost;
				if(magicCoin>_role.getMagicCoin())
				{
					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("91808"));//元宝不足
					return 
				}
			}
			_ui.seletedMc.gotoAndStop(_ui.seletedMc.currentFrame==1?2:1);
			refreshGroup(_currentBeView,BEIMPART).refreshTable(_currentBeView,BEIMPART);
			reCalculate();
		}
		/**
		 *传承丹药 
		 * 
		 */
		private function changeSeleted1():void
		{
			if(!exchangeType1)
			{
				var magicCoin : int = (exchangeType) ? magicCoinCost+magicCoinCost1 : magicCoinCost1;
				if(magicCoin>_role.getMagicCoin())
				{
					NoticeUI.getInstance().showInfo(StaticIni.getSwfLangStr("91808"));//元宝不足
					return 
				}
			}
			_ui.seleted1Mc.gotoAndStop(_ui.seleted1Mc.currentFrame==1?2:1);
			refreshGroup(_currentBeView,BEIMPART).refreshTable(_currentBeView,BEIMPART);
			reCalculate();
		}
		private function reCalculate():void
		{
			if(_currentView&&_currentBeView)
			{
				calculateBaseInfo()
				calculateGroup();
				calculateTable();
				requestForCost();
			}
		}
		private function get exchangeType():int
		{
			return _ui.seletedMc.currentFrame-1;
		}
		private function get exchangeType1():int
		{
			return _ui.seleted1Mc.currentFrame-1;
		}
		private function listenToChange():void
		{
			bundBe();
			bundIm();
		}
		private function getAddPROString(baseS:String,add:Number,percent:Boolean=false):String
		{
			return baseS+(add>0?"(+"+add+(percent?"%":"")+")":"");
		}
		private function requestForCost():void
		{
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUIST_INHERITANCE_CONSUME,exchangeType,exchangeType1,0,int(_currentView.ObjKey),int(_currentBeView.ObjKey));
		}
		private function checkCanImpart():Boolean
		{
			return true
		}
		/**
		 *发送灌顶 
		 * 
		 */		
		private function commitImpart():void
		{
			listenToChange();
			animationStart();
			_ui.commitBtn.enabled=false;
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_REQUIST_INHERITANCE,exchangeType,exchangeType1,0,int(_currentView.ObjKey),int(_currentBeView.ObjKey));
		}
		/**
		 *灌顶动画开始 
		 * 
		 */		
		private function animationStart():void
		{
//			trace("ImpartPropertyLogic.animationStart()");
//			_ui.jiantou[ANIMATION]=function():void
//			{
//				this.x=this["_x"]
//				this.y=this["_y"];
//			}
			_currentObj[ANIMATION+INDEX]=true;
//			_ui.jiantou.x=_ui.toux1.x+_ui.toux1.width;
//			TweenLite.to(_ui.jiantou,3,{x:_ui.toux2.x-_ui.jiantou.width,onComplete:animationEnd});
			_ui.jiantou.gotoAndPlay(1);
//			_ui.jiantou["mc1"].gotoAndPlay(1);
			_ui.jiantou.addFrameScript(_ui.jiantou.totalFrames-1,function():void{
				_ui.jiantou.gotoAndStop(1);
//				_ui.jiantou["mc1"].gotoAndStop(1);
				animationEnd();
			})
		}
		/**
		 *灌顶动画结束 
		 * 
		 */		
		private function animationEnd():void
		{
//			trace("ImpartPropertyLogic.animationEnd()");
			_currentObj[ANIMATION+INDEX]=false;
			_currentObj[ANIMATION]=true;
//			_ui.jiantou[ANIMATION]();
			checkMsgBox();
		}
		private function close(e:Event=null):void
		{
			_ui.hide()
		}
	}
}